package com.example.demo.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport的使用例子，LockSupport是线程等待唤醒的工具，并且不需要在同步代码块中
 * park和unpark是一对唤醒和挂起的API，且跟其调用的顺序无关，可以提前发放许可而后通知
 * 相比较Object和Lock还是会方便很多
 */

public class LockSupportDemo {
    public static void main(String[] args) {


        Thread threadA = new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "------come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "-------被唤醒");
        }, "A");
        threadA.start();


        new Thread(() -> {
            LockSupport.unpark(threadA);
            System.out.println(Thread.currentThread().getName() + "-----发放许可");
        }, "B").start();

    }
}
